﻿using System;
using System.Windows.Forms;
using EasyIot.WinApp;

namespace EasyIot.WinApp
{
    public partial class TextBoxInput : UserControl
    {
        int tabIndex = 1;
        int maxLength = 0;
        String m_strInputText = "";
        private int m_nCurSection = 0;

        public TextBoxInput()
        {
            InitializeComponent();
            txtInput.GotFocus += TxtInput_GotFocus;
        }

        private void TxtInput_GotFocus(object sender, EventArgs e)
        {
            //CDataMgr.MainHandle.OnTTKeyBoardInput(KeyType.Key_TEXTBOX_FORCECHANGE, tabIndex.ToString());
        }

        private void txtInput_MouseClick(object sender, MouseEventArgs e)
        {
            DataMgr.MainHandle.OnTTKeyBoardInput(KeyType.Key_TEXTBOX_FORCECHANGE, tabIndex.ToString());
        }

        public void Clear()
        {
            m_strInputText = "";
            txtInput.Text = "";
            txtInput.SelectionStart = 0;
        }

        public void SetDefaultValue(String val)
        {
            m_strInputText = val;
            txtInput.Text = m_strInputText;
        }

        public void SetTextBox(int index, int len)
        {
            tabIndex = index;
            maxLength = len;
            SetCenter();
            Clear();
            txtInput.Width = this.Width;
        }

        public void SetPassword()
        {
            txtInput.PasswordChar = '*';
        }

        public String GetText()
        {
            return txtInput.Text;
        }

        public void SetCenter()
        {
            txtInput.TextAlign = HorizontalAlignment.Center;
            txtInput.Focus();
        }

        public int GetMaxLen()
        {
            return maxLength;
        }

        public void SetCursor()
        {
            txtInput.Focus();
        }

        public void InputText(String strChar)
        {
            m_strInputText = txtInput.Text;// 当期数据重新赋值// 键盘输入123，再将关标切到3之前，使用软键盘上的删除，报错,解决方法：判断长度是否一致，不一致时重新赋值

            m_nCurSection = txtInput.SelectionStart;

            if ("SPACE".Equals(strChar))
            {
                if (m_nCurSection != txtInput.Text.Length)
                {
                    // 中间位置删除
                    if (0 != m_nCurSection)
                    {
                        String strLeft = m_strInputText.Substring(0, m_nCurSection - 1);
                        String strRight = m_strInputText.Substring(m_nCurSection);
                        m_strInputText = strLeft + strRight;
                        if (m_nCurSection > 0) m_nCurSection -= 1;
                    }
                }
                else
                {
                    if (m_strInputText.Length > 0)
                    {
                        m_strInputText = m_strInputText.Substring(0, m_strInputText.Length - 1);
                        m_nCurSection = m_strInputText.Length;
                    }
                }
            }
            else
            {
                // 插入
                if (maxLength == m_strInputText.Length)
                {
                    return;
                }

                if (m_nCurSection != txtInput.Text.Length)
                {
                    // 中间位置插入
                    String strLeft = m_strInputText.Substring(0, m_nCurSection) + strChar;
                    String strRight = m_strInputText.Substring(m_nCurSection);
                    m_strInputText = strLeft + strRight;
                    m_nCurSection += 1;
                }
                else
                {
                    m_strInputText += strChar;
                    m_nCurSection = m_strInputText.Length;
                }
            }
            txtInput.Text = m_strInputText;
            txtInput.SelectionStart = m_nCurSection;// 重新赋值光标位置

            SetCursor();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtInput.Clear();
            txtInput.SelectionStart = 0;
        }
    }
}
